//
//  PayResultViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/31.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "PayResultViewController.h"
#import "PayResultModel.h"
#import "OrderDetailViewController.h"
#import "MineDiscountViewController.h"
#import "MineAllOrderViewController.h"
@interface PayResultViewController ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *atitle;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UIImageView *codeImg;

@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIImageView *iv;
@property (nonatomic, strong) UILabel *reminderLabel;
@end

@implementation PayResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self addBackItem];
    
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(backItemAction)];
    
    self.title = @"支付结果";
    
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(handleCompleteBtn)];
    self.navigationItem.rightBarButtonItem = btn;
    btn.tintColor = ColorFrom16(0xF5CB37);
    
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 64, GWIDTH, 1)];
//    line.backgroundColor = COLOR_BACKGROUND;
//    [self.view addSubview:line];
    [self initView];
    [self alertView];

    [self startNetwork];
}

- (void)backItemAction
{
    MineAllOrderViewController *all = [MineAllOrderViewController new];
    all.titleName = @"待使用";
    all.status = @"1";//待使用
    all.flag = @"订单";
    [self.navigationController pushViewController:all animated:YES];
}

- (void)startNetwork
{
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/order/getOrderDet?orderSn=%@", GW_Domain, self.ordersn] parameters:@{@"orderSn":self.ordersn} completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSDictionary *dic = resultDict[@"data"];
        _atitle.text = [NSString stringWithFormat:@"产品号 %@", dic[@"goods_name"]];
        _numberLabel.text = dic[@"orderSn"];
        NSLog(@"支付成功 %@", dic);

        NSString *str = [NSString stringWithFormat:@"%@", STRING(dic[@"qccode"])];
        str = [NetWork handleUrlStr:str withDict:nil];
        NSLog(@"拼接链接 %@", str);
        
        [_codeImg sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil];
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
    }];
    
    
    ///--------
    //----------------
    NSDictionary *dic = @{@"orderSn": self.ordersn,
                          @"type"   : @"1"};//1支付成功请求 2订单详情请求
    [NetWork sendPostRequestURL:[NSString stringWithFormat:@"%@/order/getOrderDet", GW_Domain] parameters:dic completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"----支付成功调的接口 %@", resultDict);
        
        NSDictionary *dict = resultDict[@"data"];
        NSDictionary *coupons = dict[@"coupons"];
        NSLog(@"title %@", coupons[@"title"]);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if(coupons)
            {
                _bgView.hidden = NO;
                if([[NSString stringWithFormat:@"%@", coupons[@"type"]] isEqualToString:@"0"])
                {
                    _priceLabel.text = [NSString stringWithFormat:@"￥%@", coupons[@"price"]];
                    _priceLabel.attributedText = [GW_Tool setTextColor:ColorFrom16(0x5B70D8) rangeOfString:@"￥" string:_priceLabel.text textFont:20];
                    _priceLabel.textColor = ColorFrom16(0x5B70D8);
                    _priceLabel.textColor = ColorFrom16(0x526ADA);
                    _reminderLabel.text = [NSString stringWithFormat:@"满%@元可用", coupons[@"full_price"]];
                    _iv.image = IMAGE(@"privilege");
                }
                else
                {
                    _priceLabel.text = [NSString stringWithFormat:@"￥%@", coupons[@"price"]];
                    _priceLabel.attributedText = [GW_Tool setTextColor:ColorFrom16(0xFFC33E) rangeOfString:@"￥" string:_priceLabel.text textFont:20];
                    _priceLabel.textColor = ColorFrom16(0xFFC33E);
                    _priceLabel.textColor = ColorFrom16(0xE19F0D);
                    _reminderLabel.text = [NSString stringWithFormat:@"满%@元可用", coupons[@"full_price"]];
                    _iv.image = IMAGE(@"common");
                }
                
            }
        });
      
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
    }];
    
}

- (void)handleCompleteBtn
{
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"tabbarselect" object:nil];
    [self backToHomeTab:3];
}

- (void)handleBackBtn:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initView
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, 78)];
    label.text = @"购买成功";
    label.textColor = ColorFrom16(0xF5CB37);
    label.font = PFMFONT(18);
    label.textAlignment = 1;
    [self.view addSubview:label];
    
    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,  78, GWIDTH, 44)];
    [self.view addSubview:firstView];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, 1)];
    line.backgroundColor = COLOR_BACKGROUND;
    [firstView addSubview:line];
//
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 43, GWIDTH, 1)];
    line1.backgroundColor = COLOR_BACKGROUND;
    [firstView addSubview:line1];
    
    _atitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 12, GWIDTH - 20, 20)];
    _atitle.text = @"爱儿美儿童舞蹈秋季梦想儿童舞蹈";
    _atitle.textColor = ColorFrom16(0x757982);
    _atitle.font = PFRFONT(14);
    [firstView addSubview:_atitle];
    
    
    UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(0,  78 + 44, GWIDTH, 44)];
    
    [self.view addSubview:secondView];
    
    UILabel *alabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 13, 45, 20)];
    alabel.text = @"订单号";
    alabel.textColor = ColorFrom16(0x757982);
    alabel.font = PFRFONT(14);
    [secondView addSubview:alabel];
    
    _numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(85, 8, GWIDTH - 85 , 30)];
    _numberLabel.text = @"4934 9942 99";
    _numberLabel.textColor = ColorFrom16(0x757982);
    _numberLabel.font = PFRFONT(14);
    
    //设置值间距
    NSDictionary *dic = @{NSKernAttributeName:@5.f
                          };
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_numberLabel.text attributes:dic];
    _numberLabel.attributedText = attributedString;
    [secondView addSubview:_numberLabel];
    
    UIView *aline = [[UIView alloc] initWithFrame:CGRectMake(0, 43, GWIDTH, 1)];
    aline.backgroundColor = COLOR_BACKGROUND;
    [secondView addSubview:aline];
    
    
    //二维码
    _codeImg = [UIImageView new];
    _codeImg.image = IMAGE(@"");
    [self.view addSubview:_codeImg];
    [_codeImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(secondView.mas_bottom).offset(56.3);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(@150);
        make.height.equalTo(@150);
    }];
    
    UILabel *reminderLabel = [UILabel new];
    reminderLabel.text = @"向商家出示以上消费券即可消费";
    reminderLabel.textColor = ColorFrom16(0x757982);
    reminderLabel.font = PFRFONT(10);
    [self.view addSubview:reminderLabel];
    [reminderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_codeImg.mas_bottom).offset(14);
        make.centerX.equalTo(_codeImg.mas_centerX);
        make.width.equalTo(@145);
        make.height.equalTo(@14);
    }];
}

- (void)handleShareBtn
{
    ShareModel *model = [ShareModel new];
    model.title = @"购买成功";
    [[ShareView sharedInstance] showShareViewWithModel:model andCallback:^{
        
    }];
}


- (void)alertView
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT)];
    _bgView.hidden = YES;
    _bgView.backgroundColor = ColorFrom16A(0x000000, 0.8);
    [self.navigationController.view addSubview:_bgView];
 
    
    UIView *view = [UIView new];
    view.layer.backgroundColor = ColorWhite.CGColor;
    view.layer.cornerRadius = 12;
    view.layer.masksToBounds = YES;
    [_bgView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@260);
        make.height.equalTo(@231);
        make.centerX.equalTo(_bgView.mas_centerX);
        make.centerY.equalTo(_bgView.mas_centerY);
    }];
    
    _iv = [UIImageView new];
    _iv.image = IMAGE(@"");
    _iv.userInteractionEnabled = YES;
    _iv.backgroundColor = ColorFrom16(0xd8d8d8);
    [view addSubview:_iv];
    [_iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view);
        make.left.and.right.equalTo(view);
        make.height.equalTo(@99);
    }];
    
    
    _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 260, 67)];
    _priceLabel.textColor = ColorFrom16(0x5B70D8);
    _priceLabel.font = PFRFONT(40);
    _priceLabel.textAlignment = 1;
    [_iv addSubview:_priceLabel];
    
    _reminderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 77, 260, 20)];
    _reminderLabel.textColor = ColorFrom16(0xE19F0D);
    _reminderLabel.font = PFRFONT(14);
    _reminderLabel.textAlignment = 1;
    [_iv addSubview:_reminderLabel];
    
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:IMAGE(@"close") forState:0];
    [closeBtn setEnlargeEdge:20];
    [closeBtn addTarget:self action:@selector(handleCloseAction) forControlEvents:UIControlEventTouchUpInside];
    [_iv addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iv).offset(11);
        make.right.equalTo(_iv).offset(-11);
        make.width.equalTo(@11);
        make.height.equalTo(@11);
    }];
    
    UILabel *alb = [UILabel new];
    alb.text = @"派乐宝会员专享优惠券";
    alb.textColor = ColorFrom16(0xF5CB37);
    alb.font = PFRFONT(17);
    [view addSubview:alb];
    [alb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iv.mas_bottom).offset(10);
        make.centerX.equalTo(view.mas_centerX);
        make.width.greaterThanOrEqualTo(@159);
        make.height.equalTo(@22);
    }];
    
    UILabel *label = [UILabel new];
    label.text = @"下次购买商品可直接抵现\n请在“我的-优惠券”中查看";
    label.textColor = ColorFrom16(0xa7a7a7);
    label.textAlignment = 1;
    label.numberOfLines = 0;
    label.font = PFRFONT(13);
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(alb.mas_bottom).offset(10);
        make.left.equalTo(view).offset(15);
        make.right.equalTo(view).offset(-15);
        make.height.equalTo(@42);
    }];
    
    
    UIButton *gotoSee = [UIButton buttonWithType:UIButtonTypeCustom];
    [gotoSee setTitle:@"立即查看" forState:0];
    gotoSee.backgroundColor = ColorFrom16(0xFEDE6D);
    [gotoSee setTitleColor:ColorFrom16(0x323232) forState:0];
    gotoSee.titleLabel.font = PFRFONT(17);
    gotoSee.frame = CGRectMake(0, 88 + 99, 260, 44);
    [view addSubview:gotoSee];
    [gotoSee addTarget:self action:@selector(handleOtoSee) forControlEvents:UIControlEventTouchUpInside];
    //切圆角  任意切两个
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:gotoSee.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = gotoSee.bounds;
    maskLayer.path = maskPath.CGPath;
    gotoSee.layer.mask = maskLayer;
}

- (void)handleOtoSee
{
    _bgView.hidden = YES;
    MineDiscountViewController *mineVC = [MineDiscountViewController new];
    [self.navigationController pushViewController:mineVC animated:YES];
}


- (void)handleTap
{
    OrderDetailViewController *VC = [OrderDetailViewController new];
    VC.ordersn = self.ordersn;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 完成按钮响应方法
- (void)handleAction
{
    
}

- (void)handleCloseAction
{
    _bgView.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
